package com.aaa.jx.controller;

import com.aaa.jx.entity.*;
import com.aaa.jx.service.TSdsRecordDetailService;
import com.aaa.jx.service.TSdsRecordService;
import com.aaa.jx.service.TSdsSubService;
import com.aaa.jx.service.impl.TSdsRecordDetailServiceImpl;
import com.aaa.jx.service.impl.TSdsRecordServiceImpl;

import com.aaa.jx.service.impl.TSdsSubServiceImpl;
import com.util.LayData;
import com.util.LayPage;
import com.util.PrintJsonData;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * (TSdsRecord)表控制层
 *
 * @author makejava
 * @since 2023-05-05 16:52:59
 */


@WebServlet(name = "TSdsRecordServlet", value = "/TSdsRecordServlet")
public class TSdsRecordServlet extends HttpServlet {

    private TSdsRecordService tSdsRecordService = new TSdsRecordServiceImpl();
    TSdsSubService tSdsSubService=new TSdsSubServiceImpl();
    TSdsRecordDetailService tSdsRecordDetailService=new TSdsRecordDetailServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        TSdsRecord tSdsRecord=new TSdsRecord();
        try {
            BeanUtils.populate(tSdsRecord,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        // 
        String method = req.getParameter("method");
        if (method==null){
            List<TSdsSub> tSdsSubs = tSdsSubService.queryBySub("");
            long count = tSdsSubService.count(null);
            LayData layData=new LayData(count,tSdsSubs);
            req.setAttribute("subs",layData);
            req.getRequestDispatcher("viwes/jx/problem.jsp").forward(req,resp);
        }else if(method.equals("query")){
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            LayPage layPage=new LayPage(page,limit);
            layPage.setStart(layPage.getStart());
            List<TSdsRecord> tSdsRecords = tSdsRecordService.queryByPage(null, layPage);
            long count = tSdsRecordService.count(null);
            LayData success=new LayData(0,"",count,tSdsRecords);
            PrintJsonData.printData(resp,success);
            return;
        }else if(method.equals("addrecord")){
            //获取前台登录信息，封装到对象中

            //获取当前时间

            //将做题记录存入数据库
            TSdsRecord record = tSdsRecordService.queryByTel(tSdsRecord.getTel());
            LayData success=new LayData();
            if (record == null){
                TSdsRecord insert = tSdsRecordService.insert(tSdsRecord);
                success.setData(insert.getId());
            }else {
                success.setData(record.getId());
            }
            PrintJsonData.printData(resp,success);
            //查出存入数据库的记录id
//            TSdsRecord record = tSdsRecordService.queryByTel(tSdsRecord.getTel());
//            req.setAttribute("recordid",record.getId());
//            //查出所有题目，并存入req
//            List<TSdsSub> tSdsSubs = tSdsSubService.queryBySub("");
//            long count = tSdsSubService.count(null);
//            LayData layData=new LayData(count,tSdsSubs);
//            req.setAttribute("subs",layData);
//            //跳转到做题界面
//            req.getRequestDispatcher("viwes/jx/problem.jsp").forward(req,resp);
            return;
        }else if(method.equals("upd")){
            tSdsRecordService.update(tSdsRecord);
            LayData success=new LayData( 0L,null);
            PrintJsonData.printData(resp,success);
            return;
        }else if(method.equals("checktel")){
            String tel = req.getParameter("tel");
            // 1. 数据库中该号码是否已经做题   如果
            TSdsRecord record = tSdsRecordService.queryByTel(tel);
            System.out.println(record);
            LayData layData = new LayData();
            if(record==null){
                layData.setCode(0);
            }else {
                List<SdsDetail> sdsDetails = tSdsRecordDetailService.queryById(record.getId());
                System.out.println(sdsDetails);
                if (sdsDetails == null||sdsDetails.size() == 0){
                    layData.setCode(0);
                }else {
                    layData.setCode(1);
                }
            }
            PrintJsonData.printData(resp,layData);
            // del(req,resp);
            return;
        }else if(method.equals("del")){
            tSdsRecordService.deleteById(tSdsRecord.getId());
            LayData success=new LayData(0,"",0L,null);
            PrintJsonData.printData(resp,success);
            //del(req,resp);
            return;
        }

    }


}




